package com.novel.app.view.storys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dongdxiang.framework.BaseActivity;
import com.dongdxiang.framework.http.ImageLoader;
import com.dongdxiang.framework.http.QjResult;
import com.google.gson.reflect.TypeToken;
import com.novel.app.CommonUtils;
import com.novel.app.R;
import com.novel.app.utils.Configs;
import com.novel.app.view.storys.bean.LikeNovelEntity;
import com.novel.app.view.storys.bean.StoryDetailEntity;
import com.novel.app.view.storys.bean.StoryEntity;
import java.util.List;

public class StoryDetailActivity extends BaseActivity {

    private LinearLayout like_novel;
    private LikeNovelEntity likeNovelEntity;
    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_details);
        setCenterTitle("书城");
        setLeft_Btn("返回",R.mipmap.s6,onClickListener);
        like_novel = (LinearLayout)findViewById(R.id.like_novel);
        String id = getIntent().getStringExtra("id");
        doNet(id);
        getLike(false);
    }

    private void doNet(String id){
        String url = Configs.story+id;
        httpGetRequest(0,url,new TypeToken<QjResult<StoryDetailEntity>>() {},true,true);
    }

    private void getLike(boolean isLoading){
        String url = Configs.guessYouLike;
        httpGetRequest(1,url,new TypeToken<QjResult<LikeNovelEntity>>() {},true,isLoading);
    }

    public void btn_Click(View view){
        switch (view.getId()){
            case R.id.start:
                break;
            case R.id.save:
                break;
            case R.id.zhangjie:
                break;
            case R.id.chapters:
                break;
            case R.id.change:
                getLike(true);
                break;
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_left:
                    finish();
                    break;
            }
        }
    };
    private CommonUtils.NovelClickListenter novelClickListenter = new CommonUtils.NovelClickListenter() {
        @Override
        public void goToNovel(String id) {
            doNet(id);
        }
    };

    @Override
    protected void onSucceed(int what, Object response) {
        if (response != null){
            if (what == 0){
                StoryDetailEntity storyDetailEntity = (StoryDetailEntity)response;
                if (storyDetailEntity == null) return;
                ((TextView)findViewById(R.id.novel_name)).setText(storyDetailEntity.getName());
                ((TextView)findViewById(R.id.novel_author)).setText(storyDetailEntity.getAuthor());
                ((TextView)findViewById(R.id.novel_type)).setText(storyDetailEntity.getCate_name()+" | "+ (storyDetailEntity.getIs_finished() == 1 ? "已完结":"连载中"));
                ((TextView)findViewById(R.id.novel_date)).setText("最后更新时间："+storyDetailEntity.getLast_edited());
                ((TextView)findViewById(R.id.novel_lastChapter)).setText(storyDetailEntity.getChapter_name());
                ((TextView)findViewById(R.id.instro)).setText(storyDetailEntity.getIntro());
                ((TextView)findViewById(R.id.chapters)).setText("查看目录（"+storyDetailEntity.getChapter_num()+"）");
                ImageLoader.disImage(baseActivity,storyDetailEntity.getImg_url(),(ImageView) findViewById(R.id.img));
            }else if(what == 1){
                likeNovelEntity = (LikeNovelEntity) response;
                if (likeNovelEntity == null) return;
                List<StoryEntity.NovelEntity> list = likeNovelEntity.getData();
                if (list == null || list.size() == 0) return;
                like_novel.removeAllViews();
                CommonUtils.createView(baseActivity,like_novel,list,novelClickListenter);
            }
        }

    }

    @Override
    protected void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {

    }
}
